<template>
	<view class="content">
		<view class="uni-flex uni-column" style="justify-content: center; align-items: center;">
			<image class="uni-logo" src="http://img.deitui.com/?w=200&h=200&text=Logo&fontsize=36&textclor=fff&bgclor=ed6d53"></image>
		</view>
		<view class="uni-cell">
			<view class="uni-cell__value">
				<input type="number" v-model="username" placeholder="请输入手机号码">

			</view>
			<view class="uni-icon uni-icon-close1 uni-padding-top-15 uni-padding-bottom-15 " custom-class="uni-cell__right-icon" v-if="username.length > 0"></view>

		</view>
		<view class="uni-cell">
			<view class="uni-cell__value">
				<input type="password"  v-model="code" v-if="passwordType=='password'" placeholder="请输入验证码">
				<input type="text" v-model="code" v-if="passwordType=='text'" placeholder="请输入验证码">
			</view>
			<view class="uni-icon uni-icon-eye uni-text-gray  uni-padding-top-15 uni-padding-bottom-15" style="font-size: :32upx;" custom-class="uni-cell__right-icon" v-if="passwordType == 'text'" @click="changePasswordType"></view>
			<view class="uni-icon uni-icon-eye-close uni-text-gray  uni-padding-top-15 uni-padding-bottom-15" style="font-size: :32upx;" custom-class="uni-cell__right-icon" v-if="passwordType == 'password'" @click="changePasswordType"></view>
			<view class="uni-icon uni-icon-close1  uni-padding-top-15 uni-padding-bottom-15 " custom-class="uni-cell__right-icon"  v-if="code.length>0" @click="clearPassword" ></view>
			<view class="uni-timer-button" v-if="Verification" @click="handleClick">点击获取验证码</view>
			<view class="uni-timer-button" v-if="!Verification">{{timer}}秒后重新获取</view>
		</view>
		<view class="btn-row">
			<button type="primary" class="uni-bg-default" @tap="bindRegister">注册</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	
	import {getRegistryCode,validateRegistryCode} from '@/api/ucenter/index'

	export default {
		data() {
			return {
				providerList: [],
				hasProvider: false,
				Verification: true, //通过v-show控制显示获取还是倒计时
				timer: 60, //定义初始时间为60s
				username: '',
				code: '',
				passwordType: "password",
				positionTop: 0,
				delta: 1
			}
		},
		computed: mapState(['forcedLogin']),

		methods: {

			initPosition() {
				/**
				 * 使用 absolute 定位，并且设置 bottom 值进行定位。软键盘弹出时，底部会因为窗口变化而被顶上来。
				 * 反向使用 top 进行定位，可以避免此问题。
				 */
				this.positionTop = uni.getSystemInfoSync().windowHeight - 100;
			},
			clearPassword() {
				this.password = ""
			},
			handleClick() {
				getRegistryCode(this.username).then(response => {
					if(response.status == 200 && response.rel == true) {
						this.Verification = false; //点击button改变v-show的状态
						let auth_timer = setInterval(() => { //定时器设置每秒递减
							this.timer--; //递减时间
							if (this.timer <= 0) {
								this.timer = 60
								this.Verification = true; //60s时间结束还原v-show状态并清除定时器
								clearInterval(auth_timer)
							}
						}, 1000);
					}
				}).catch(err => {
					uni.showModal({
						title:"提示",
						content: err.errorType
					})
				});
			},
			changePasswordType() {
				this.passwordType = (this.passwordType == 'password' ? 'text': 'password')
			},
			bindRegister() {
				/**
				 * 客户端对账号信息进行一些必要的校验。
				 * 实际开发中，根据业务需要进行处理，这里仅做示例。
				 */
				if (this.username.length < 5) {
					uni.showToast({
						icon: 'none',
						title: '账号最短为 5 个字符'
					});
					return;
				}
				console.log("code:"+this.code.length)
				
				if (this.code.length != 4) {
					uni.showToast({
						icon: 'none',
						title: '验证码为4个字符'
					});
					return;
				}
				/**
				 * 下面简单模拟下服务端的处理
				 * 检测用户账号密码是否在已注册的用户列表中
				 * 实际开发中，使用 uni.request 将账号信息发送至服务端，客户端在回调函数中获取结果信息。
				 */
				const data = {
					mobile: this.username,
					code: this.code
				};
				
				validateRegistryCode(data).then(response => {
					if(response.status == 1) {
						goTo()
					} else {
						uni.showModal({
							title: "提示",
							content: response.message
						})
					}
				}).catch(err => {
					uni.showModal({
						title:"提示",
						content: err.errorType
					})
				});

			},
			goto() {
				let params = "typeId=2&userLoginId="+this.username+ "delta="+this.delta
				uni.navigateBack({
					url: pages[index]+"?"+ params
				})
			}
		},
		onLoad(e) {
			this.initPosition();
			if(e.delta !== undefined) {
				this.delta = this.delta + e.delta
			}
		}
	}
</script>

<style>

	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}

	.flex-center {
		text-align: center;
		justify-content: center;
		align-items: center;
	}

	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}

	.action-row navigator {
		color: #7C4BF9;
		padding: 0 20px;
	}


/* 
	.input-flex-txt {
		flex: 1;
		line-height: 36px;
		color: #646464;
	} */
</style>
